package com.xuyuan.json.jackson.generic;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.xuyuan.json.jackson.custom.Bodys;
import com.xuyuan.json.jackson.entity.Person;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import org.junit.jupiter.api.Test;

import java.math.BigDecimal;
import java.util.List;
import java.util.UUID;

/**
 * Jackson提供的泛型反序列化支持
 *
 * @author xuyuan
 */
public class JacksonGenericStudy {
    private final String json = "[{\n" + "    \"id\": null,\n" + "    \"name\": \" \",\n" + "    \"age\": 500,\n" + "    \"gender\": false,\n" + "    \"email\": \"email\",\n" + "    \"employed\": true,\n" + "    \"salary\": 10\n" + "}]";
    private final ObjectMapper MAPPER = new ObjectMapper();

    @Test
    public void test() throws Exception {
        List<UserResource> list = MAPPER.readValue(json, new TypeReference<List<UserResource>>() {
        });
        list.forEach(System.out::println);
    }

    @Test
    public void test1() throws Exception {
        Bodys<Person> personBodys = new Bodys<>();
        personBodys.setCommonField("1111");
        Person person = new Person();
        person.setMyName("xxx");
        person.setMyHeight(172);
        personBodys.setBody(person);
        String jsonStr = MAPPER.writeValueAsString(personBodys);
        System.out.println();

        Bodys<Person> personBodys1 = MAPPER.readValue(jsonStr, new TypeReference<Bodys<Person>>() {
        });
        System.out.println(personBodys1);
    }

    @Data
    @ToString
    @NoArgsConstructor
    @AllArgsConstructor
    class UserResource {
        private UUID id;
        private String name;
        private int age;
        private boolean gender;
        private String email;
        private boolean employed;
        private BigDecimal salary;
    }

}


